package com.srpingde.hhh;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.jiekou.HHHIml;

public class UserActivity extends Activity {
	TextView showname,show_welcome;
	LinearLayout nologin;
	HHHIml weiQuIml;
	//TextView 
	Button denglu_iv;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user);
		showname = (TextView) findViewById(R.id.show_name);
		nologin = (LinearLayout) findViewById(R.id.user_rl);
		show_welcome = (TextView) findViewById(R.id.show_welcome);
		denglu_iv = (Button) findViewById(R.id.denglu_iv);//变成按钮了。并且加点击功能
		denglu_iv.setOnClickListener(new OnClickListener() { 
			@Override 
			   public void onClick(View v) { 
				startActivity(new Intent(UserActivity.this, LoginActivity.class));
			}
		});
		weiQuIml = new HHHIml();
		layout();
		if(!HhhApp.getInstance().getName().equals("")){
			showname.setText("欢迎"+HhhApp.getInstance().getUname()+"登录");
			show_welcome.setVisibility(View.GONE);
			denglu_iv.setVisibility(View.GONE);
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(!HhhApp.getInstance().getUname().equals("")){
			showname.setVisibility(View.VISIBLE);
			showname.setText("欢迎"+HhhApp.getInstance().getUname());//+"登录");
			show_welcome.setVisibility(View.GONE);
			denglu_iv.setVisibility(View.GONE);
		}else{
			show_welcome.setVisibility(View.VISIBLE);
			show_welcome.setText("游客、欢迎来到唯趣。");
			showname.setVisibility(View.GONE);
			denglu_iv.setVisibility(View.VISIBLE);
		}
	}

	public void user(View v) {
		switch (v.getId()) {
		case R.id.user_rl:
			if(!HhhApp.getInstance().getUname().equals("")){
				return;
			}else{
				startActivity(new Intent(this,LoginActivity.class));
			}
			break;
		case R.id.user_shoucangjia:
			if(!HhhApp.getInstance().getUname().equals("")){
				startActivity(new Intent(UserActivity.this, ShouCangJiaActivity.class));
			}else{
				start();
			}
			break;
		case R.id.user_gouwuche:
			if(!HhhApp.getInstance().getUname().equals("")){
				startActivity(new Intent(UserActivity.this, GouWuCheActivity.class));
			}else{
				start();
			}
			break;
		case R.id.user_shouhuo:
			if(!HhhApp.getInstance().getUname().equals("")){
				startActivity(new Intent(UserActivity.this, ShouHuoListActivity.class));
			}else{
				start();
			}
			break;
		case R.id.user_dingdan:
			if(!HhhApp.getInstance().getUname().equals("")){
				startActivity(new Intent(UserActivity.this, DingDanActivity.class));
			}else{
				start();
			}
			break;
		}
	}
	private void start(){
		new AlertDialog.Builder(this).setTitle("提示").setMessage("请先登录").setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				startActivity(new Intent(UserActivity.this, LoginActivity.class));
			}
		}).create().show();
	}
	private void layout() {
		// 头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		
		RelativeLayout.LayoutParams params = (LayoutParams) nologin.getLayoutParams();
		params.height = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[0] * 0.2);
		nologin.setLayoutParams(params);
		
		itemLayout((Button) findViewById(R.id.user_shoucangjia));
		itemLayout((Button) findViewById(R.id.user_gouwuche));
		itemLayout((Button) findViewById(R.id.user_shouhuo));
		itemLayout((Button) findViewById(R.id.user_dingdan));
		
	}
	private void itemLayout(Button  textView){
		LinearLayout.LayoutParams layoutParams = (android.widget.LinearLayout.LayoutParams) textView.getLayoutParams();
		layoutParams.height = (int) (HhhApp.getInstance().title_h()*1.5);
		textView.setLayoutParams(layoutParams);
	}
}
